package com.ahao.servlet.course;

import com.ahao.model.Course;
import com.ahao.model.RespBean;
import com.ahao.service.CourseService;
import com.ahao.utils.CommonsUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.x.protobuf.MysqlxCrud;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/course")
public class CourseServlet extends HttpServlet {

    CourseService courseService = new CourseService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if("page".equals(action)){
            req.getRequestDispatcher("/WEB-INF/jsp/course/courseList.jsp").forward(req, resp);
        }else if("data".equals(action)){
            List<Course> list = courseService.getAllCourse();
            ObjectMapper om = new ObjectMapper();
            String json = om.writeValueAsString(list);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(json);
        }else if ("data_from_teacher".equals(action)) {
            String gid = req.getParameter("gid");
            List<Course> list = courseService.getCoursesByGid(gid);
            ObjectMapper om = new ObjectMapper();
            String json = om.writeValueAsString(list);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(json);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String courseName = req.getParameter("courseName");
        Integer integer = courseService.addCourse(courseName);
        resp.setContentType("application/json;charset=utf-8");
        RespBean respBean = null;
        if (integer == CommonsUtils.UPDATE_SUCCESS) {
            respBean = RespBean.ok("添加成功!");
        }else if(integer == CommonsUtils.UPDATE_FAIL){
            respBean = RespBean.error("课程已存在，添加失败!");
        }else {
            respBean = RespBean.error("添加失败!");
        }

        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(respBean);
        resp.getWriter().write(json);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        Integer integer = courseService.deleteCourseByCid(Integer.parseInt(cid));

        resp.setContentType("application/json;charset=utf-8");
        RespBean respBean = null;
        if (integer == CommonsUtils.UPDATE_SUCCESS) {
            respBean = RespBean.ok("删除成功!");
        }else if(integer == CommonsUtils.UPDATE_FAIL){
            respBean = RespBean.error("删除失败");
        }else if (integer == CommonsUtils.ID_NOT_FIND){
            respBean = RespBean.error("非法请求!删除失败");
        }else if (integer == CommonsUtils.OTHER_EXCEPTION){
            respBean = RespBean.error("异常!删除失败");
        }else {
            respBean = RespBean.error("删除失败!");
        }
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(respBean);
        resp.getWriter().write(json);
    }
}
